/**
 * 行政区域字典
 */
$(function() {
	obj.init();
})
var tt = null;
var obj = {
	init : function() {
		// 检索
		$('#search_btn').on('click', function(e) {
			$(e.target).prop("disabled", true);
			tt.ajax.reload(function() {
				$(e.target).prop("disabled", false);
			}, false);
		});
		
		// 初始化表格
		obj.drawDatatable();
	},
	drawDatatable : function() {
		if (tt != null) {
			return;
		}
		tt = $('#tt').DataTable(
				{
					// paging : true,
					// lengthMenu: [[5, 10, -1], [5, 10, "All"]],
					// lengthChange : false,
					pageLength : 10,
					pagingType : 'numbers',
					searching : false,
					info : true,
					scrollCollapse : true,
					autoWidth : true,
					processing : true,
					serverSide : true,
					dom : 'tipl',
					ordering : true,
					order : [ [ 1, 'desc' ] ],
					language : {
						url : $('#datatables_i18n').prop("src")
					},
					// deferRender: true,
					ajax : {
						url : $('#tt').data('href'),
						type : "POST",
						data : function(d) {
							d.q_search_key = $('#q_search_key').val();
							d.q_year = $('#q_year').val();
							d.q_include_disable = $('#q_include_disable').is(
									':checked');
						}
					},
					rowCallback : function(row, data, index) {
						var info = tt.page.info();
						
						if (typeof (info) != 'undefined') {
							$('td:eq(0)', row).html(index + info.start + 1);
						} else {
							$('td:eq(0)', row).html(index + 1);
						}
					},
					rowId : 'id',
					columns : columns
				});
		$('#tt').on('draw.dt', function() {
		});

		$('#tt tbody').on('click', 'tr', function(e) {
			if (e.target.type == 'button') {
				tt.$('tr.selected').removeClass('selected');
				$(this).addClass('selected');
			}
		});
		// 标记脏数据确认
		var deleteConfirmBtnHandler = function() {
			$.ajax({
				type : "POST",
				url : $('#delete-modal').data('url'),
				data : {
					id : $('#delete-modal').data('id')
				},
				success : function(data) {
					$.dialog.tips(data.message);
					$('#delete-modal').modal('hide');
					$('#search_btn').trigger('click');
				},
				error : ajaxerror
			})
		}
		$('#delete-modal #confirm-btn').on('click', deleteConfirmBtnHandler);
		var redBtnHandler = function(e) {
			var cur_data = tt.row($(this).parents('tr')).data();
			var text = "确定要标记【" + cur_data.name + '(' + cur_data.areaOne
					+ ")】数据吗?";
			$('#delete-modal #info').text(text);
			$('#delete-modal .modal-title').text($(e.target).attr('title'));
			$('#delete-modal').data('id', cur_data.id);
			$('#delete-modal').data('url', $(e.target).data('href'));
			$('#delete-modal').modal('show');
		};
		$('#tt tbody').on('click', '.bg-red', redBtnHandler);
		var greanBtnHandler = function(e) {
			if (e.target.id == 'edit_btn') {
				var cur_data = tt.row($(this).parents('tr')).data();
				$('#form-modal').data('url', $(e.target).data('href'));
				var title = '修改' + cur_data.name + '(' + cur_data.areaOne + ')';
				$('#form-modal .modal-title').html(title);
				$('#form-modal').data('id', cur_data.id);

				$('#form-modal #area_one').val(cur_data.areaOne);
				$('#form-modal #area_two').val(cur_data.areaTwo);
				$('#form-modal #area_three').val(cur_data.areaThree);
				$('#form-modal #area_type_code').val(cur_data.areaTypeCode);
				$('#form-modal #name').val(cur_data.name);
				$('#form-modal #area_code').val(cur_data.areaCode);
				$('#form-modal #zip_code').val(cur_data.zipCode);
				$('#form-modal').modal('show');
			}
		};
		$('#tt tbody').on('click', '.bg-green', greanBtnHandler);

	},

}
